/**
 *  通用ajax 访问接口
 *  需要jquery，vue.js， login.js提供支持.
 */

var RESULTCODE = {};
RESULTCODE.SUCCESS = [200 , 201 , 202 , 203 , 204 , 205 , 206];		// 请求成功
RESULTCODE.FAIL = [100 , 101 , 102 , 103 , 104 , 105 , 106];		// 请求失败
RESULTCODE.LOGON_FAILURE = 301;		// 登录失效
RESULTCODE.INVALID_REQUEST = 302;	// 链接无效
RESULTCODE.NO_AUTHORITY = 303;		// 无权限

var Ajax = {
	defaults : {
		type:'POST',
		url: basePath + "/" , 
		data:{},
		timeout:30*1000,
		silence:false,
		success: function (respone){
			/*vm = vm|| top.vm;*/
			if ( RESULTCODE.SUCCESS.join(',').indexOf(respone.code) != -1 ) {
				!this.silence&&message( this.message || respone.msg  , 'success');
				this.callback&&this.callback(respone.data ,respone.length , respone );
			} else if ( RESULTCODE.FAIL.join(',').indexOf(respone.code) != -1 ) {
				message( respone.msg );
			} else if (respone.code == RESULTCODE.LOGON_FAILURE) { 
				loginDialog();
			} else {
				message( respone.msg );
			}
//			console.log( '请求url：' + this.url + '，时间：' + new Date().Format("yyyy-MM-dd HH:mm:ss") + '，结果：' + respone.msg + '，数据长度：' + (respone.length || 0) );
			this.finallyd&&this.finallyd(respone.data ,respone.length , respone );
		},
		fail: function (respone) {
//			console.log( '请求url：' + this.url + '，时间：' + new Date().Format("yyyy-MM-dd HH:mm:ss") + '，结果：请求超时！' );
			//message('网络异常，请求失败');
			this.finallyd&&this.finallyd(respone.data ,respone.length , respone );
		},
		error:function ( respone , params2 ,params3 ) {
//			console.log( '请求url：' + this.url + '，时间：' + new Date().Format("yyyy-MM-dd HH:mm:ss") + '，结果：' + params3.message );
			//message('网络异常，请稍后再试');
			this.finallyd&&this.finallyd(respone.data ,respone.length , respone );
		}
	},
	go:function ( options ){
		$.ajax( $.extend({} , Ajax.defaults , options) );
	},
	list:function ( options ) {
		this.go($.extend({ silence: true }, options, { type: 'POST',data: options.data } ));
	},
	get:function ( options ){
		this.go( $.extend({silence:true} , options , { type:'GET' } ));
	},
	query:function ( options ){
		this.go( $.extend({silence:true} , options , { type:'GET' } ));
	},
	del:function ( options ){
		this.go( $.extend({} , options , { type:'DELETE' } ));
	},
	add:function ( options ){
		this.go( $.extend({} , options , { type:'POST' } ));
	}, 
	execute:function ( options ){
		this.go( $.extend({} , options , { type:'PUT' } ));
	}, 
	update:function ( options ){
		this.go( $.extend({} , options , { type:'PUT' } ));
	},
	drop:function ( options ){
		this.go( $.extend({} , options , { type:'DELETE' } ));
	},
	login:function (options){
		this.go( $.extend({} , options , { type:'POST' } ));
	},
	exported:function (options) {
		
	}
}
